package base.utils;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.EncoderException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

@SuppressWarnings("restriction")
public class CreateFlies {

	//创建文件夹
	public static File create(String myappPath) throws Exception {
		File filename = new File(myappPath);
		// 如果文件不存在，则创建一个新文件
		if (!filename.isFile()) {
			try {
				filename.mkdir();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return filename;
	}

	//上传单个图片
		@SuppressWarnings("unused")
		public static String upload(String shopcode,String myappPath,HttpServletRequest request)
				throws Exception {
			File outFile = null;
			String picpath = "";
			String pic = "";
			String localpath = "upload/";
			try {
				if (request instanceof MultipartHttpServletRequest) {
					MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
					MultipartFile file = multipartRequest.getFiles("imgFile").get(0);
					if(file!=null){
					String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));//取得后缀
					String time = new  SimpleDateFormat("yyyyMMddHHmmss").format(new Date());//使用时间作为要保存的文件名
					Image image =ImageIO.read(file.getInputStream());
					BufferedImage tag = new BufferedImage(200,150,BufferedImage.TYPE_INT_RGB);
					picpath = localpath + shopcode+ "/"+time + suffix;
					outFile = new File(myappPath + "/"+ time + suffix);
					if (!outFile.exists()) {
						outFile.createNewFile();
						pic = outFile.getAbsolutePath();
					}
					FileOutputStream out = new FileOutputStream(outFile);
					tag.getGraphics().drawImage(image.getScaledInstance(200, 150, Image.SCALE_SMOOTH),0,0,null);
					JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
					encoder.encode(tag);
					out.close();
				}

					}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return picpath;
		}
		//上传单个图片(商品和富文本)
				@SuppressWarnings("unused")
				public static Map<String,String> upload(int width,int height, String ymd,String dirName,String myappPath,HttpServletRequest request)
						throws Exception {
					Map<String, String> a=new HashMap<String, String>();
					File outFile = null;
					String picpath = "";
					String pic = "";
					String localpath = "upload/"+dirName+"/"+ymd;
					try {
						if (request instanceof MultipartHttpServletRequest) {
							MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
							MultipartFile file = multipartRequest.getFiles("imgFile").get(0);
							
							if(file!=null){
							String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));//取得后缀
							String time = new  SimpleDateFormat("yyyyMMddHHmmssS").format(new Date());//使用时间作为要保存的文件名
							BufferedImage b =ImageIO.read(file.getInputStream());
							if(width==150&&height==150){
								width=b.getWidth();
								height=b.getHeight();
							}
							Image image =ImageIO.read(file.getInputStream());
							BufferedImage tag = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
							
							picpath = localpath +  "/"+time + suffix;
							outFile = new File(myappPath + time + suffix);
							if (!outFile.exists()) {
								outFile.createNewFile();
								pic = outFile.getAbsolutePath();
							}
							FileOutputStream out = new FileOutputStream(outFile);
							tag.getGraphics().drawImage(image.getScaledInstance(width,height, Image.SCALE_SMOOTH),0,0,null);
							JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
							encoder.encode(tag);
							out.close();
							a.put("pic_name",localpath +  "/"+time + suffix );
							a.put("pic", myappPath +time + suffix);
						}

							}
					} catch (Exception e) {
						e.printStackTrace();
					}
					return a;
				}
		//上传单个图片
		@SuppressWarnings("unused")
		public static String upload(String shopcode,String myappPath,HttpServletRequest request,MultipartFile file)
				throws Exception {
			File outFile = null;
			String picpath = "";
			String pic = "";
			String localpath = "upload/";
			try {
				if (request instanceof MultipartHttpServletRequest) {
					
				
					if(file!=null&&file.getSize()>0){
						
						String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));//取得后缀
						String time = new  SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());//使用时间作为要保存的文件名
						BufferedImage image =ImageIO.read(file.getInputStream());
						
					BufferedImage tag = new BufferedImage(image.getWidth(),image.getHeight(),BufferedImage.TYPE_INT_RGB);
					picpath = localpath + shopcode+ "/"+time + suffix;
				
					File filename = new File(myappPath +"/"+shopcode +"/");
					// 如果文件不存在，则创建一个新文件
					if (!filename.isFile()) {
						try {
							filename.mkdir();
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
					outFile = new File(myappPath +"/"+shopcode +"/"+ time + suffix);
					if (!outFile.exists()) {
						outFile.createNewFile();
						pic = outFile.getAbsolutePath();
					}
					FileOutputStream out = new FileOutputStream(outFile);
					tag.getGraphics().drawImage(image.getScaledInstance(image.getWidth(),image.getHeight(), Image.SCALE_SMOOTH),0,0,null);
					JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
					encoder.encode(tag);
					out.close();
				}

					}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return picpath;
		}
		
		
		//上传单个图片
				@SuppressWarnings("unused")
				public static String upload(String myappPath,HttpServletRequest request,MultipartFile file)
						throws Exception {
					File outFile = null;
					String picpath = "";
					String pic = "";
					String localpath = "upload/";
					try {
						if (request instanceof MultipartHttpServletRequest) {
							
						
							if(file!=null&&file.getSize()>0){
								
								String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));//取得后缀
								String time = new  SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());//使用时间作为要保存的文件名
								BufferedImage image =ImageIO.read(file.getInputStream());
								
							BufferedImage tag = new BufferedImage(image.getWidth(),image.getHeight(),BufferedImage.TYPE_INT_RGB);
							picpath = localpath +"/"+ file.getOriginalFilename();
						
							File filename = new File(myappPath +"/");
							// 如果文件不存在，则创建一个新文件
							if (!filename.isFile()) {
								try {
									filename.mkdir();
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
							outFile = new File(myappPath +"/"+file.getOriginalFilename());
							if (!outFile.exists()) {
								outFile.createNewFile();
								pic = outFile.getAbsolutePath();
							}
							FileOutputStream out = new FileOutputStream(outFile);
							tag.getGraphics().drawImage(image.getScaledInstance(image.getWidth(),image.getHeight(), Image.SCALE_SMOOTH),0,0,null);
							JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
							encoder.encode(tag);
							out.close();
						}

							}
					} catch (Exception e) {
						e.printStackTrace();
					}
					return picpath;
				}
		
		
//		//上传单个图片（修改图片空间图片用）
				@SuppressWarnings("unused")
				public static String upload(String myappPath,MultipartFile file,HttpServletRequest request)
						throws Exception {
					File outFile = null;
					String picpath = "";
					String pic = "";
					ServletContext application = request.getSession().getServletContext();

					String savePath = application.getRealPath("/") ;
					System.out.println("myappPath:"+myappPath);
					try {
							if(file!=null){
							String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));//取得后缀
							String time = new  SimpleDateFormat("yyyyMMddHHmmssS").format(new Date());//使用时间作为要保存的文件名
							BufferedImage image =ImageIO.read(file.getInputStream());
							System.out.println(image.getHeight()+"gao");
							BufferedImage tag = new BufferedImage(image.getWidth(),image.getHeight(),BufferedImage.TYPE_INT_RGB);
							picpath = myappPath +time + suffix;
							outFile = new File(savePath+myappPath +  time + suffix);
							System.out.println("123"+savePath+myappPath +  time + suffix);
							if (!outFile.exists()) {
								outFile.createNewFile();
								pic = outFile.getAbsolutePath();
							}
							FileOutputStream out = new FileOutputStream(outFile);
							tag.getGraphics().drawImage(image.getScaledInstance(image.getWidth(),image.getHeight(), Image.SCALE_SMOOTH),0,0,null);
							JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
							encoder.encode(tag);
							out.close();
						}

					} catch (Exception e) {
						e.printStackTrace();
					}
					return picpath;
				}

	   //上传单个图片
		@SuppressWarnings("unused")
		public static String upload1(String shopcode,String myappPath,HttpServletRequest request)
				throws Exception {
			File outFile = null;
			String licencepath = "";
			String pic = "";
			String localpath = "comm/upload/";
			try {
				if (request instanceof MultipartHttpServletRequest) {
					MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
					MultipartFile file = multipartRequest.getFiles("licencepath").get(0);
					long size = file.getSize();
					byte[] data = new byte[(int) size];
					InputStream input = file.getInputStream();
					input.read(data);
					licencepath = localpath + shopcode
							+ "/" + file.getOriginalFilename();
					outFile = new File(myappPath + File.separator
							+ file.getOriginalFilename());
					if (!outFile.exists()) {
						outFile.createNewFile();
						pic = outFile.getAbsolutePath();
					}
					FileOutputStream outStream = new FileOutputStream(outFile);
					outStream.write(data);
					outStream.close();
					input.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return licencepath;
		}
		public static String pic(String a) {
			
						
			return null;
		}
}
